package com.UdpRecieve.service.impl;

import com.UdpRecieve.tool.StringUtils;
import com.UdpRecieve.domain.system.SysUser;
import com.UdpRecieve.framework.ExceptionBusiness.LoginException;
import com.UdpRecieve.framework.constant.ServletConstant;
import com.UdpRecieve.framework.jedis.JedisComponent;
import com.UdpRecieve.mapper.SysUserMapper;
import com.UdpRecieve.service.SysLoginService;
import com.UdpRecieve.tool.CookieUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

@Service
public class SysLoginServiceImpl implements SysLoginService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    JedisComponent jedisComponent;
    @Override
    public List<SysUser> selectUser(SysUser user) {
        return sysUserMapper.selectUser(user);
    }

    /**
     * 登录逻辑
     * @param user
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    public void doLogin(SysUser user, HttpServletRequest request, HttpServletResponse response) throws Exception {

        SysUser sysUser = new SysUser();
        sysUser.setUserName(user.getUserName());
        List<SysUser> userNameList =  selectUser(sysUser);
        //判断用户名是否存在
        if (userNameList==null||userNameList.isEmpty()){
            throw new LoginException("该用户名不存在!");
        }
        List<SysUser> userList =  selectUser(user);
        //判断用户名密码是否匹配
        if (userList==null||userList.isEmpty()){
            throw new LoginException("用户名或密码不正确!");
        }
        //登录成功，生成token
        String token = UUID.randomUUID().toString();
        //把用户设置进Session中
        request.getSession().setAttribute(ServletConstant.SESSION_KEY+token,user.getUserName());
        //写cookie
        CookieUtils.setCookie(request, response, ServletConstant.WEB_TOKEN, token);
        //System.out.println("session超时时间为:"+request.getSession().getServletContext().getSessionTimeout());
    }

    /**
     * 检查在同一个浏览器下是否重复登录
     * @param request
     * @param response
     */
    @Override
    public Boolean isLogin(HttpServletRequest request, HttpServletResponse response) {
        String token = CookieUtils.getCookieValue(request,ServletConstant.WEB_TOKEN);
        if (StringUtils.isEmpty(token)){
            return false;
        }
        String useName = (String) request.getSession().getAttribute(ServletConstant.SESSION_KEY+token);
        if (useName==null){
            return false;
        }
        return true;
    }

    /**
     * 检测不同浏览器下是否重复登录
     * @param user
     * @param request
     * @param response
     */
    @Override
    public void isLoginOnOtherWeb(SysUser user, HttpServletRequest request, HttpServletResponse response) throws Exception {
         //判断当前登录的用户是否在,在线用户名录里
        if (!StringUtils.isEmpty(user.getUserName())&&
                ServletConstant.OnlineUserList.size()!=0&&
                ServletConstant.OnlineUserList.get(user.getUserName())!=null){
            throw new LoginException("该用户已经在其他浏览器登录,请退出后再登录!");
        }
    }

    /**
     * 退出登录
     * @param user
     * @param request
     * @param response
     */
    @Override
    public void loginOut(HttpServletRequest request, HttpServletResponse response) {
        //把该用户从在线用户列表中删除
        //如果销毁的是指定用户的sessionid,就把该用户移出在线用户列表中
        ServletConstant.OnlineUserList.entrySet().removeIf(entry -> entry.getKey() != request.getSession().getId());
        //删除session
        request.getSession().removeAttribute(ServletConstant.SESSION_KEY+CookieUtils.getCookieValue(request,ServletConstant.WEB_TOKEN));
        //删除cookie
        CookieUtils.deleteCookie(request,response,ServletConstant.WEB_TOKEN);
    }


}
